{% extends "basic.html" %}
{% block head %}
    {{ super() }}
    <script type="text/javascript">
        function softwareUpdate(id) {
            softname = $('#softname_' + id).val();
            softpath = $('#softpath_' + id).val();
            $.ajax({
                url: '/interface/software/update',
                type: "POST",
                data: {id: id, softname:softname, softpath:softpath},
                success: function (result) {
                    alert(result);
                }
            });
        }

        function software_display(result) {
            $('#tbody_software_display').html('')
            soft_objs = JSON.parse(result);
            for (i = 0; i < soft_objs.length; i++) {
                id = soft_objs[i]['id']
                softname = soft_objs[i]['softname']
                softpath = soft_objs[i]['softpath']
                $('#tbody_software_display').append('<tr>' +
                        '<td><input id="softname_' + id + '" style="border: none;outline: none; box-shadow:none" type="text" value="' + softname + '" /></td>' +
                        '<td><input id="softpath_' + id + '" style="border: none;outline: none; box-shadow:none" type="text" value="' + softpath + '" /></td>' +
                        '<td><a href="javascript:softwareDel(' + id + ')">删除</a>   <a href="javascript:softwareUpdate(' + id + ')">确认修改</a></td>' +
                        '</tr>');
            }
        }

        function softwareDel(id) {
            if (confirm('确定要删除吗？')) {
                $.ajax({
                    url: '/interface/software/delete',
                    type: "POST",
                    data: {id: id},
                    success: function (result) {
                        software_display(result);
                    }
                });
            }
        }

        $(document).ready(function () {

            $.ajax({
                url: '/interface/software/select_all',
                type: "GET",
                success: function (result) {
                    software_display(result);
                }
            });

            $('#button_add_software').click(function () {
                input_softname = $('#input_softname').val();
                input_softpath = $('#input_softpath').val();
                if(!input_softname || !input_softpath){
                    alert('请输入显示名称和软件名称！');
                    return false;
                }
                $.ajax({
                    url: '/interface/software/add',
                    type: "POST",
                    data: {softname: input_softname, softpath: input_softpath},
                    success: function (result) {
                        software_display(result);
                    }
                });
            });
        });
    </script>
{% endblock %}
{% block content %}
    <div class="container">
        <div class="col-md-4">
            <input id="input_softname" type="text" class="form-control" placeholder="请输入显示名称"/>
            <input id="input_softpath" type="text" class="form-control" placeholder="请输入软件名称"/>
            <button id="button_add_software" type="button" class="btn btn-default">增加软件列表</button>
        </div>
    </div><br />

    <div class="container">
        <div class="col-md-12">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>显示名称</th>
                    <th>软件名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="tbody_software_display">
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}